const {
    Mysql
} = require('../tools/utils');
const moment = require('moment');
module.exports = async (ctx) => {
    
    let {
        openId,
        formId,
        uid
    } = ctx.query;
    let now = Date.now();
    let startTime = moment().format('YYYYMMDDHHmmss');
    let endTime = moment(now).add(3, "days").format('YYYYMMDDHHmmss');
    let sql = `REPLACE INTO formId(openId,uid,formId,save_time,expires) VALUES('${openId}',(SELECT uid from cSessionInfo where open_id='${openId}'),'${formId}',${startTime},${endTime})`;
    try {
        let res = await Mysql(sql);
        if (res) {
            let sql = `INSERT INTO intergal(uid,score) VALUES(${uid},2) ON DUPLICATE KEY UPDATE uid=${uid},score=score+2`;
            try {
                let res = await Mysql(sql);
                ctx.state.data = {
                    msg: 'جۇغلانمىغا ئېرىشڭىتىز',
                    code: '001'
                }
            } catch (error) {
                ctx.state.data = {
                    msg: 'مەغلۇپ بولدى، قايتا سىناڭ',
                    code: '002'
                }
            }
        }
    } catch (error) {
        ctx.state.data = {
            msg: 'مەغلۇپ بولدى، قايتا سىناڭ',
            code: '002'
        }
    }

}